package cn.xz.gobang.util

import android.util.Log
import cn.xz.gobang.bean.ChessPData
import cn.xz.gobang.bean.ClickData
import java.util.*

/**
 * 判断是否胜利工具*/
object ClickWinUtil {

    private var mData0: ArrayList<ClickData> = arrayListOf() //黑棋所落
    private var mData1: ArrayList<ClickData> = arrayListOf() //白旗所落

    //是否胜利回调
    interface OnClickWinListener {
        fun success(type: Int)
    }

    fun to(mOData: ArrayList<ClickData>, mAll: MutableList<ChessPData>,onClickWinListener: OnClickWinListener){
        val mData: ArrayList<ClickData> = arrayListOf()
        mData.addAll(mOData)
        // 根据下标从小到大排序
        Collections.sort(mData, Comparator<ClickData> { p1, p2 ->
            p1.position - p2.position
        })
        //区分黑棋白旗
        mData0.clear()
        mData1.clear()
        mData.forEach {
            if (it.type == 0){
                mData0.add(it)
            }else{
                mData1.add(it)
            }
        }
        //判断是否有五子连棋
        hasSame(0,mData0,mAll,onClickWinListener)
        hasSame(1,mData1,mAll,onClickWinListener)
    }

    //过滤是否有相同的棋子
    private fun hasSame(type: Int,mData: ArrayList<ClickData>,mAll: MutableList<ChessPData>,onListener: OnClickWinListener){
        //五个开始计算
        if (mData.size > 4){
            mData.forEach {
                val mP = it.position
                //横向
                val isHWin = isH(type,mP,mAll,onListener)
                if (isHWin){
                    return
                }
                //竖向
                val isSWin = isS(type,mP,mAll,15,onListener)
                if (isSWin){
                    return
                }
                //左上-右下
                val isZSWin = isS(type,mP,mAll,16,onListener)
                //val isZSWin = isZS(mP,mAll)
                if (isZSWin){
                    return
                }
                //右上-左下
                val isYXWin = isS(type,mP,mAll,14,onListener)
                //val isYXWin = isYX(mP,mAll)
                if (isYXWin){
                    return
                }
            }
        }
    }


    private fun isH(type: Int,mP: Int,mAll: MutableList<ChessPData>,onListener: OnClickWinListener): Boolean{
        var isWin = false
        var mLe = mP - 4
        if (mLe % 15 == 0 || mLe < 0){
            mLe = mP - 3
            if (mLe% 15 == 0  || mLe < 0 ){
                mLe = mP - 2
                if (mLe% 15 == 0 || mLe < 0 ){
                    mLe = mP - 1
                    if (mLe% 15 == 0 || mLe < 0 ){
                        mLe = mP
                    }
                }
            }
        }
        var mRi = mP + 4
        if ((mRi + 1) % 15 == 0 || mRi > 224 ){
            mRi = mP + 3
            if((mRi + 1) % 15 == 0 || mRi > 224){
                mRi = mP + 2
                if((mRi + 1) % 15 == 0 || mRi > 224){
                    mRi = mP + 1
                    if((mRi + 1) % 15 == 0 || mRi > 224){
                        mRi = mP
                    }
                }
            }
        }
        var mIndex = 0
        for(i in mLe..mRi){
            if (!isWin){
                if (mAll[i].type == type){
                    mIndex += 1
                }else{
                    mIndex = 0
                }
                if (mIndex == 5){
                    var is0 = true
                    if (type == 1){
                        is0 = false
                    }
                    onListener.success(type)
                    Log.e("TAG", "hasSame: -->${is0}棋获胜-横向")
                    isWin = true
                }
            }

        }
        return isWin
    }


    private fun isS(type: Int,mP: Int,mAll: MutableList<ChessPData>,where: Int,onListener: OnClickWinListener): Boolean{
        var isWin = false
        var mTop = mP - 4 * where
        if (mTop < 0){
            mTop = mP - 3 * where
            if (mTop < 0){
                mTop = mP - 2 * where
                if (mTop < 0){
                    mTop = mP - 1 * where
                    if (mTop < 0){
                        mTop = mP
                    }
                }
            }
        }
        var mBottom = mP + 4 * where
        if (mBottom > 225){
            mBottom = mP + 3 * where
            if (mBottom > 225){
                mBottom = mP + 2 * where
                if (mBottom > 225){
                    mBottom = mP + 1 * where
                    if (mBottom > 225){
                        mBottom = mP
                    }
                }
            }
        }
        var i = mTop
        var mIndex = 0
        while (i < mBottom) {
            if (mAll[i].type == type){
                mIndex += 1
            }else{
                mIndex = 0
            }
            i += where
            if (mIndex == 5){
                var mStr = "竖直"
                when(where){
                    14->{
                        mStr = "右上，左下"
                    }
                    16->{
                        mStr = "左上，右下"
                    }
                }
                var is0 = true
                if (type == 1){
                    is0 = false
                }
                onListener.success(type)
                Log.e("TAG", "hasSame: -->${is0}棋获胜-${mStr}")
                isWin = true
            }
        }

        return isWin
    }
}